import { Byte } from '../../api/io/Byte';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**属性枚举*/
export const enum E_LegionMember {
	/** 玩家ID*/
	PLAYER_ID = 0,
	/** 军团ID*/
	LEGION_ID = 1,
	/** 职位*/
	POSITION = 2,
	/** 加入时间*/
	JOIN_TIME = 3,
	/** 离线时间*/
	OFFLINE_TIME = 4,
	/**昵称*/
	NICKNAME = 5,
	/**头像索引*/
	HEAD_ICON = 6,
	/**战力*/
	FIGHT_SCORE = 7,
	/** 总贡献*/
	TOTAL_CONTRIBUTION = 8,
	/** 本周贡献*/
	WEEK_CONTRIBUTION = 9,
	/**是否在线*/
	IS_ONLINE = 10,
	/**等级*/
	LEVEL = 11,
	/**枚举数量*/
	EnumCount = 12
}


/***/
export class LegionMember {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_LEGION_MEMBER_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_LEGION_MEMBER_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: LegionMember[] = [];

	/**创建入口*/
	public static create(json?: string): LegionMember {
		const c = this.POOL.pop() || new LegionMember();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/** 玩家ID (uint32)*/
	protected _playerId: number;
	/** 军团ID (int32)*/
	protected _legionId: number;
	/** 职位 (uint8)*/
	protected _position: number;
	/** 加入时间 (long)*/
	protected _joinTime: number;
	/** 离线时间 (long)*/
	protected _offlineTime: number;
	/**昵称*/
	protected _nickname: string;
	/**头像索引 (uint8)*/
	protected _headIcon: number;
	/**战力 (long)*/
	protected _fightScore: number;
	/** 总贡献 (uint32)*/
	protected _totalContribution: number;
	/** 本周贡献 (uint32)*/
	protected _weekContribution: number;
	/**是否在线*/
	protected _isOnline: boolean;
	/**等级 (uint32)*/
	protected _level: number;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (LegionMember.POOL.indexOf(this) === -1) {
			LegionMember.POOL.push(this);
		}
	}

	/** 玩家ID*/
	public get playerId() {
		return this._playerId;
	}


	/** 玩家ID*/
	public set playerId(v: number) {
		this._playerId = v;
	}


	/** 军团ID*/
	public get legionId() {
		return this._legionId;
	}


	/** 军团ID*/
	public set legionId(v: number) {
		this._legionId = v;
	}


	/** 职位*/
	public get position() {
		return this._position;
	}


	/** 职位*/
	public set position(v: number) {
		this._position = v;
	}


	/** 加入时间*/
	public get joinTime() {
		return this._joinTime;
	}


	/** 加入时间*/
	public set joinTime(v: number) {
		this._joinTime = v;
	}


	/** 离线时间*/
	public get offlineTime() {
		return this._offlineTime;
	}


	/** 离线时间*/
	public set offlineTime(v: number) {
		this._offlineTime = v;
	}


	/**昵称*/
	public get nickname() {
		return this._nickname;
	}


	/**昵称*/
	public set nickname(v: string) {
		this._nickname = v;
	}


	/**头像索引*/
	public get headIcon() {
		return this._headIcon;
	}


	/**头像索引*/
	public set headIcon(v: number) {
		this._headIcon = v;
	}


	/**战力*/
	public get fightScore() {
		return this._fightScore;
	}


	/**战力*/
	public set fightScore(v: number) {
		this._fightScore = v;
	}


	/** 总贡献*/
	public get totalContribution() {
		return this._totalContribution;
	}


	/** 总贡献*/
	public set totalContribution(v: number) {
		this._totalContribution = v;
	}


	/** 本周贡献*/
	public get weekContribution() {
		return this._weekContribution;
	}


	/** 本周贡献*/
	public set weekContribution(v: number) {
		this._weekContribution = v;
	}


	/**是否在线*/
	public get isOnline() {
		return this._isOnline;
	}


	/**是否在线*/
	public set isOnline(v: boolean) {
		this._isOnline = v;
	}


	/**等级*/
	public get level() {
		return this._level;
	}


	/**等级*/
	public set level(v: number) {
		this._level = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_LegionMember): any {
		switch (k) {
			case E_LegionMember.PLAYER_ID: return this._playerId;
			case E_LegionMember.LEGION_ID: return this._legionId;
			case E_LegionMember.POSITION: return this._position;
			case E_LegionMember.JOIN_TIME: return this._joinTime;
			case E_LegionMember.OFFLINE_TIME: return this._offlineTime;
			case E_LegionMember.NICKNAME: return this._nickname;
			case E_LegionMember.HEAD_ICON: return this._headIcon;
			case E_LegionMember.FIGHT_SCORE: return this._fightScore;
			case E_LegionMember.TOTAL_CONTRIBUTION: return this._totalContribution;
			case E_LegionMember.WEEK_CONTRIBUTION: return this._weekContribution;
			case E_LegionMember.IS_ONLINE: return this._isOnline;
			case E_LegionMember.LEVEL: return this._level;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_LegionMember, v: any): void {
		switch (k) {
			case E_LegionMember.PLAYER_ID: this._playerId = v; break;
			case E_LegionMember.LEGION_ID: this._legionId = v; break;
			case E_LegionMember.POSITION: this._position = v; break;
			case E_LegionMember.JOIN_TIME: this._joinTime = v; break;
			case E_LegionMember.OFFLINE_TIME: this._offlineTime = v; break;
			case E_LegionMember.NICKNAME: this._nickname = v; break;
			case E_LegionMember.HEAD_ICON: this._headIcon = v; break;
			case E_LegionMember.FIGHT_SCORE: this._fightScore = v; break;
			case E_LegionMember.TOTAL_CONTRIBUTION: this._totalContribution = v; break;
			case E_LegionMember.WEEK_CONTRIBUTION: this._weekContribution = v; break;
			case E_LegionMember.IS_ONLINE: this._isOnline = v; break;
			case E_LegionMember.LEVEL: this._level = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		// 玩家ID
		this._playerId = 0;
		// 军团ID
		this._legionId = 0;
		// 职位
		this._position = 0;
		// 加入时间
		this._joinTime = 0;
		// 离线时间
		this._offlineTime = 0;
		//昵称
		this._nickname = '';
		//头像索引
		this._headIcon = 0;
		//战力
		this._fightScore = 0;
		// 总贡献
		this._totalContribution = 0;
		// 本周贡献
		this._weekContribution = 0;
		//是否在线
		this._isOnline = false;
		//等级
		this._level = 0;
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._playerId) {
			json.playerId = this._playerId;
		}
		if (this._legionId) {
			json.legionId = this._legionId;
		}
		if (this._position) {
			json.position = this._position;
		}
		if (this._joinTime) {
			json.joinTime = this._joinTime;
		}
		if (this._offlineTime) {
			json.offlineTime = this._offlineTime;
		}
		if (this._nickname) {
			json.nickname = this._nickname;
		}
		if (this._headIcon) {
			json.headIcon = this._headIcon;
		}
		if (this._fightScore) {
			json.fightScore = this._fightScore;
		}
		if (this._totalContribution) {
			json.totalContribution = this._totalContribution;
		}
		if (this._weekContribution) {
			json.weekContribution = this._weekContribution;
		}
		if (this._isOnline) {
			json.isOnline = this._isOnline;
		}
		if (this._level) {
			json.level = this._level;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.playerId) {
			this._playerId = json.playerId;
		}
		if (json.legionId) {
			this._legionId = json.legionId;
		}
		if (json.position) {
			this._position = json.position;
		}
		if (json.joinTime) {
			this._joinTime = json.joinTime;
		}
		if (json.offlineTime) {
			this._offlineTime = json.offlineTime;
		}
		if (json.nickname) {
			this._nickname = json.nickname;
		}
		if (json.headIcon) {
			this._headIcon = json.headIcon;
		}
		if (json.fightScore) {
			this._fightScore = json.fightScore;
		}
		if (json.totalContribution) {
			this._totalContribution = json.totalContribution;
		}
		if (json.weekContribution) {
			this._weekContribution = json.weekContribution;
		}
		if (json.isOnline) {
			this._isOnline = json.isOnline;
		}
		if (json.level) {
			this._level = json.level;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		byte.writeUint32(this._playerId);
		byte.writeInt32(this._legionId);
		byte.writeUint8(this._position);
		byte.writeLongNumber(this._joinTime);
		byte.writeLongNumber(this._offlineTime);
		byte.writeString(this._nickname);
		byte.writeUint8(this._headIcon);
		byte.writeLongNumber(this._fightScore);
		byte.writeUint32(this._totalContribution);
		byte.writeUint32(this._weekContribution);
		byte.writeBool(this._isOnline);
		byte.writeUint32(this._level);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._playerId = byte.readUint32();
		this._legionId = byte.readInt32();
		this._position = byte.readUint8();
		this._joinTime = byte.readLongNumber();
		this._offlineTime = byte.readLongNumber();
		this._nickname = byte.readString();
		this._headIcon = byte.readUint8();
		this._fightScore = byte.readLongNumber();
		this._totalContribution = byte.readUint32();
		this._weekContribution = byte.readUint32();
		this._isOnline = byte.readBool();
		this._level = byte.readUint32();
		return this;
	}


	/**克隆*/
	public clone(): LegionMember {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = LegionMember.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = LegionMember.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			// 玩家ID
			if (idx === E_LegionMember.PLAYER_ID) {
				const oldv = this._playerId;
				this._playerId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._playerId, oldv);
				i += 2;
				continue;
			}
			// 军团ID
			if (idx === E_LegionMember.LEGION_ID) {
				const oldv = this._legionId;
				this._legionId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._legionId, oldv);
				i += 2;
				continue;
			}
			// 职位
			if (idx === E_LegionMember.POSITION) {
				const oldv = this._position;
				this._position = us[i + 1];
				IAsync.dispatch(eventID, idx, this._position, oldv);
				i += 2;
				continue;
			}
			// 加入时间
			if (idx === E_LegionMember.JOIN_TIME) {
				const oldv = this._joinTime;
				this._joinTime = us[i + 1];
				IAsync.dispatch(eventID, idx, this._joinTime, oldv);
				i += 2;
				continue;
			}
			// 离线时间
			if (idx === E_LegionMember.OFFLINE_TIME) {
				const oldv = this._offlineTime;
				this._offlineTime = us[i + 1];
				IAsync.dispatch(eventID, idx, this._offlineTime, oldv);
				i += 2;
				continue;
			}
			//昵称
			if (idx === E_LegionMember.NICKNAME) {
				const oldv = this._nickname;
				this._nickname = us[i + 1];
				IAsync.dispatch(eventID, idx, this._nickname, oldv);
				i += 2;
				continue;
			}
			//头像索引
			if (idx === E_LegionMember.HEAD_ICON) {
				const oldv = this._headIcon;
				this._headIcon = us[i + 1];
				IAsync.dispatch(eventID, idx, this._headIcon, oldv);
				i += 2;
				continue;
			}
			//战力
			if (idx === E_LegionMember.FIGHT_SCORE) {
				const oldv = this._fightScore;
				this._fightScore = us[i + 1];
				IAsync.dispatch(eventID, idx, this._fightScore, oldv);
				i += 2;
				continue;
			}
			// 总贡献
			if (idx === E_LegionMember.TOTAL_CONTRIBUTION) {
				const oldv = this._totalContribution;
				this._totalContribution = us[i + 1];
				IAsync.dispatch(eventID, idx, this._totalContribution, oldv);
				i += 2;
				continue;
			}
			// 本周贡献
			if (idx === E_LegionMember.WEEK_CONTRIBUTION) {
				const oldv = this._weekContribution;
				this._weekContribution = us[i + 1];
				IAsync.dispatch(eventID, idx, this._weekContribution, oldv);
				i += 2;
				continue;
			}
			//是否在线
			if (idx === E_LegionMember.IS_ONLINE) {
				const oldv = this._isOnline;
				this._isOnline = us[i + 1];
				IAsync.dispatch(eventID, idx, this._isOnline, oldv);
				i += 2;
				continue;
			}
			//等级
			if (idx === E_LegionMember.LEVEL) {
				const oldv = this._level;
				this._level = us[i + 1];
				IAsync.dispatch(eventID, idx, this._level, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in LegionMember with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: LegionMember): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		if (this._playerId !== other._playerId) {
			updates.push(E_LegionMember.PLAYER_ID, this._playerId);
		}
		if (this._legionId !== other._legionId) {
			updates.push(E_LegionMember.LEGION_ID, this._legionId);
		}
		if (this._position !== other._position) {
			updates.push(E_LegionMember.POSITION, this._position);
		}
		if (this._joinTime !== other._joinTime) {
			updates.push(E_LegionMember.JOIN_TIME, this._joinTime);
		}
		if (this._offlineTime !== other._offlineTime) {
			updates.push(E_LegionMember.OFFLINE_TIME, this._offlineTime);
		}
		if (this._nickname !== other._nickname) {
			updates.push(E_LegionMember.NICKNAME, this._nickname);
		}
		if (this._headIcon !== other._headIcon) {
			updates.push(E_LegionMember.HEAD_ICON, this._headIcon);
		}
		if (this._fightScore !== other._fightScore) {
			updates.push(E_LegionMember.FIGHT_SCORE, this._fightScore);
		}
		if (this._totalContribution !== other._totalContribution) {
			updates.push(E_LegionMember.TOTAL_CONTRIBUTION, this._totalContribution);
		}
		if (this._weekContribution !== other._weekContribution) {
			updates.push(E_LegionMember.WEEK_CONTRIBUTION, this._weekContribution);
		}
		if (this._isOnline !== other._isOnline) {
			updates.push(E_LegionMember.IS_ONLINE, this._isOnline);
		}
		if (this._level !== other._level) {
			updates.push(E_LegionMember.LEVEL, this._level);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(LegionMember.TOTAL_EVENT_ID, this.getV(0));
	}

}
